package com.neo.sk.util

import models.Tables.MoSimpleDetailRow
import org.jsoup.Jsoup
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer

/**
 * User: Huangshanqi
 * Date: 2015/7/13
 * Time: 10:20
 */
object ParseUtil {

  //解析所有的标签种类
  def parseTagHtml(content:String):List[(String,String)] = {
    val result = new ListBuffer[(String,String)]()
    val doc = Jsoup.parse(content)
    if(doc!=null){
      val tag_clouds = doc.getElementsByClass("tag_cloud")
      if(tag_clouds!=null&&tag_clouds.size()>0){
        val tags = tag_clouds.get(0).getElementsByTag("a")
        if(tags!=null&&tags.size()>0){
          for(tag <- tags){
            val tuple = (tag.attr("href"),tag.text)
            result += tuple
          }
        }
      }
    }
    result.toList
  }

  //解析某个标签种类的电影列表，获取电影url和电影id
  def parseMovieUrlList(content:String):List[(String,Long)] = {
    val result = new ListBuffer[(String,Long)]()
    val doc = Jsoup.parse(content)
    if(doc!=null){
      val movie_list = doc.getElementsByClass("movie-list")
      if(movie_list!=null&&movie_list.size()>0){
        val dds = movie_list.get(0).getElementsByTag("dt")
        if(dds!=null&&dds.size()>0){
          for(dt <- dds){
           val as = dt.getElementsByTag("a")
            if(as!=null&&as.size()>0){
              //http://movie.douban.com/subject/1291843/?from=tag_all
              val url = as.get(0).attr("href")
              val urlPattern = "(.*)/subject/(\\d+)(.*)".r
              val urlPattern(_,mid,_) = url
              val tuple = (url,mid.toLong)
              result += tuple
            }
          }
        }
      }
    }
    result.toList
  }

//  //解析豆瓣电影移动版
//  def parseSimpleMovie(content:String,url:String):MoSimpleDetailRow = {
//
//    //http://movie.douban.com/subject/6874403/mobile
//    val midPattern = "(.*)/subject/(\\d+)/mobile(.*)".r
//    val midPattern(_,mid,_) = url
//
//    val doc = Jsoup.parse(content)
//    if(doc!=null){
//      val header = doc.getElementsByTag("header")
//      val movieName = header!=null&&header.size()>0 match {
//        case true => header.get(0).text.replaceAll(" ","").trim
//        case false => "unknow"
//      }
//      val base = doc.getElementById("base")
//      if(base!=null){
//        val p = base.select("p.rank")
//        val (score,scorePeopleNum) = p!=null&&p.size()>0 match {
//          case true =>{
//            val textPattern = "(.*)(\\d+)\\s+(\\d+)(.*)".r
//            val textPattern(_,score,scorePeopleNum,_) =  p.get(0).text
//            (score,scorePeopleNum)
//          }
//          case false => (0.toString,0.toString)
//        }
//
//        val lis = base.getElementsByTag("li")
//
//      }
//
//    }
//    null
//  }


}
